<?php

namespace app\common\service\order\pipeline\coupon;

use app\model\CouponOrder;
use app\model\CouponProductChannel;
use think\Exception;

/**
 * 权益商品才验证商品是否存在
 */
class ProductInfo
{

    public static function handle(CouponOrder $couponOrder, $next)
    {

        $params = $couponOrder->params;
        $serv_type = $params['serv_type'] ?? '';

        if ($serv_type != PRODUCT_COUPON_SERV) {
            return $next($couponOrder);
        }

        $product_channel_id = $params['product_channel_id'] ?? '';
        $couponOrder->quote_id = $product_channel_id;

        if (empty($couponOrder->couponProductChannel)){

            throw new Exception('产品不存在');

        }

        if ($couponOrder->couponProductChannel->status != CouponProductChannel::STATUS_ACTIVE){
            throw new Exception('产品信息错误');
        }

        $couponOrder->product_id = $couponOrder->couponProductChannel->product_id;
        $couponOrder->brand_id = $couponOrder->couponProductChannel->brand_id;


        //推广类型为了后台我的推广统计
        $extension_type = input('extension_type',0);

        if ($extension_type){
            $couponOrder->extension_type = $extension_type;
        }

        $next($couponOrder);
    }
}